﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;


using Microsoft.Web.Services3;
using Microsoft.Web.Services3.Security.Tokens;

namespace IncubatorServices
{
    /// <summary>
    /// Summary description for IncubatorService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [Policy("ServerPolicy")]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class IncubatorService : System.Web.Services.WebService
    {

        [WebMethod]
        public User GetUser(int id)
        {
            //User u = new User(0, "test", "user", 0, "testuser", "pass", "adsad", 0);
            User u = new User();
            u.LoadData(id);
            return u;
        }
        [WebMethod]
        public void UpdateUser(User user)
        {
            user.storeData();
        }


        [WebMethod]
        public int VerifyUser(string username, string password)
        {
            //returns user id if user is verified correctly
            
           
            return Security.verifyUser(username, password);

        }
        
        [WebMethod]
        public User RegisterUser(string fname, string lname, string username, string password, int role_id)
        {
            //User u = new User(0, fname, lname, 0, username, password, "", role_id);
            User u = new User();
            u.RegisterNewUser(fname, lname, 0, username, password, role_id);
            //u.SaveData()
            return u;
        }

        [WebMethod]
        public Company CreateCompany(string cname, string description, int money, User user)
        {
            Company c = new Company();
            c.RegisterNewCompany(cname, money, description);

            //update user
            user.company_id = c.id;
            user.storeData();

            //create shares for company
            Share s = new Share();
            s.RegisterNewShares(c, user, 100, false, -1);
            return c;
        }


        [WebMethod]
        public Company GetCompany(int id)
        {
            Company c = new Company();
            c.LoadData(id);
            return c;
        }

        [WebMethod]
        public Quiz GetQuiz(int id)
        {   
            //TODO: check if user owns the quiz first!
            Quiz q = new Quiz();
            q.LoadData(id);
            return q;
        }
        [WebMethod]
        public bool BuyQuiz(Company c, Quiz q)
        {
            return q.BuyQuiz(c);
        }
        [WebMethod]
        public bool AnswerQuestion(Company c, Quiz quiz, QuizQuestion question, string answer)
        {
            if (c.quiz_bought.Contains(quiz.id))
            {//user does not own the quiz
                return quiz.AnswerQuestion(c, question, answer);
            }
            return false;
            //if(
            //TODO: check if user owns quiz AND hasnt answered this question yet
            //TODO: check if answer is correct, if it is, add answer to user's money
            //TODO: update quiz status to say one extra question answerd
        }
        [WebMethod]
        public QuizStatus GetQuizStatus(Company c, Quiz q)
        {
            QuizStatus qs = new QuizStatus();
            qs.LoadData(q, c);
            return qs;
        }

        [WebMethod]
        public List<Quiz> GetAllQuizzes()
        {
            List<Quiz> q = new List<Quiz>();
            q = GenericHelper.GetAllQuizzes();
            return q;
        }

        //MARKETPLACE
        [WebMethod]
        public List<Share> GetAllUserShares(User u)
        {
            return Share.GetAllUserShares(u);
        }
        [WebMethod]
        public List<Share> GetAllCompanyShares(Company c)
        {
            return Share.GetAllCompanyShares(c);
        }
        [WebMethod]
        public List<Share> GetMarketplaceShares()
        {

            return Share.GetAllMarketShares();
        }
        [WebMethod]
        public Share GetShares(User u, Company c, bool onMarket)
        {
            Share share = new Share();
            share.GetShares(c, u, onMarket, -1); 
            return share;
        }
        [WebMethod]
        public bool BuyShares(User new_owner, User old_owner, Company c, int numShares, int cost_per_item)
        {
            Share.transferShares(c, old_owner, new_owner, numShares, cost_per_item);
            return true;
        }
        [WebMethod]
        public void PutShareOnMarket(User owner, Company c, int num_shares, int cost_per_share)
        {
            Share.PutOnMarket(c, owner, num_shares, cost_per_share);
        }
        private void checkRegUser()
        {
            if (Security.isRegUser()) throw new Exception("Registration user can only register new users.");
        }
        private void checkAdmin(){

            if (!Security.isAdmin()) throw new Exception("User must be an administrator to use this function");
        }
        [WebMethod]
        public List<User> GetAllUsers()
        {//TODO: requires admin access
            checkAdmin();
            return IncubatorServices.User.getAllUsers();
            
        }

        [WebMethod]
        public void DeleteUser(User u)
        {//TODO: Requires admin access
            checkAdmin();
            u.deleteUser();
        }

        [WebMethod]
        public void SetUserRole(User u, int role)
        {//TODO: Requires admin access
            checkAdmin();
            u.role_id = role;
            u.storeData();
        }

        [WebMethod]
        public void SetMoney(Company c, int money)
        {//TODO: Requires admin access
            checkAdmin();
            c.money = money;
            c.storeData();
        }

        [WebMethod]
        public User GetFounder(Company c)
        {
            User u = new User();
            return u;
        }

    }
}
